<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/24
 * Time: 15:43
 */

namespace Admin\Controller;
header('Content-type: text/html; charset=utf8');
use Common\Controller\AuthController;

class TotalController extends AuthController {

    public function total_list(){

        $obj = D('B2c');
        $list = $obj->table('xf_total_table')->field('id,title')->select();
        $data_year = date('Y')+5;
        $year = [];
        for ($i = 0; $i < 11 ; $i++) {
            $year[] = $data_year - $i;
        }
        $this->assign('year',$year);
        $this->assign('list',$list);
        $this->display();

    }

    public function table_down(){
        $tableid = I('tableid');
        $quarter = I('quarter');
        $year = I('year');
        if ($tableid == 1){
            $this->total_one($tableid,$year,$quarter);
        }elseif ($tableid ==2){
            $this->total_two($tableid,$year,$quarter);
        }elseif ($tableid ==3){
            $this->total_three($tableid,$year,$quarter);
        }
    }

    public function create_table(){
        $tableid = I('tableid');
        $quarter = I('quarter');
        $year = I('year');
        if ($tableid == 1){
            $this->maoyi($tableid,$year,$quarter);
        }elseif ($tableid ==2){
            $this->gongye($tableid,$year,$quarter);
        }elseif ($tableid ==3){
            $this->wuliu($tableid,$year,$quarter);
        }
        $this->assign('tableid',$tableid);
        $this->assign('quarter',$quarter);
        $this->assign('year',$year);
        $this->display();
    }
    public function maoyi($tableid,$year,$quarter){
        $obj = D('B2c');
        $data = $obj->table('xf_total_table')->where('id=1')->find();
        $oldarr = json_decode(trim($data['info']),true);
        $tableid = $tableid;
        $con2['year'] = $year;
        $con2['quarter'] = $quarter;
        $con2['code'] ='CDTLG417';
        $con2['state'] = 3;
        $con1['year'] = $year;
        $con1['quarter'] = $quarter;
        $con1['code'] ='CDTLG416';
        $con1['state'] = 3;

        //查询出两张表所需的记录
        $res1 = $obj->table('xf_total')->where($con1)->order('code asc')->select();
        $res2 = $obj->table('xf_total')->where($con2)->order('code asc')->select();
        if (empty($res1) && empty($res2)){
            $this->error('该季度暂无汇总数据',0,0);
        }
        $arr1 = array();
        $arr2 = array();
        //将第一种表数据加入一种数组中
        foreach ($res1 as $k=>$v){
            array_push($arr1,json_decode($v['finnal_info'],true));
        }
        //16表集合结果
        $arr = array();
        $pregarrone = array();
        $pregarrtwo = array();
        foreach ($arr1 as $key=>$value){
            foreach ($value as $k=>$v){
                foreach ($v as $aa=>$ss){
                    //根据键名进行赋值
                    if ($aa=='last' && $ss!='--'){
                        if (is_numeric($ss)){
                            $arr[$k][$aa] += $ss;
                        }else{
                            //去除重复
                            $pp = array_filter(explode('、',$ss));
                            foreach ($pp as $ww){
                                if (!in_array($ww,$pregarrone[$k])){
                                    $pregarrone[$k][] = $ww;
                                    $arr[$k][$aa] .= $ww.'、';
                                }
                            }
                        }

                    }
                    if ($aa=='now' && $ss!='--'){
                        if (is_numeric($ss)){
                            $arr[$k][$aa] += $ss;
                        }else{
                            $tt = array_filter(explode('、',$ss));
                            foreach ($tt as $ww){
                                if (!in_array($ww,$pregarrtwo[$k])){
                                    $pregarrtwo[$k][] = $ww;
                                    $arr[$k][$aa] .= $ww.'、';
                                }
                            }
                        }
                    }

                }

            }

        }

        //17表集合
        $array = array();
        foreach ($res2 as $k=>$v){
            array_push($arr2,json_decode($v['finnal_info'],true));
        }
        foreach ($arr2 as $aa=>$bb){

            foreach ($bb as $key=>$value){
                foreach ($value as $k=>$v){
                    if ($k=='last' && $v!='--'){
                        if (is_numeric($v)){
                            $array[$key][$k] += $v;
                        }else{
                            $tt = array_filter(explode('、',$v));
                            foreach ($tt as $rr){
                                if (!in_array($rr,$pregarrone[$key])){
                                    $pregarrone[$key][] = $rr;
                                    $array[$key][$k] .= $rr.'、';
                                }
                            }

                        }

                    }
                    if ($k=='now' && $v!='--'){
                        if (is_numeric($v)){
                            $array[$key][$k] += $v;
                        }else{
                            $ll = array_filter(explode('、',$v));
                            foreach ($ll as $hh){
                                if (!in_array($hh,$pregarrtwo[$key])){
                                    $pregarrtwo[$key][] = $hh;
                                    $array[$key][$k] .= $hh.'、';
                                }
                            }
                        }
                    }
                }
            }
        }
        //合并数据
        $oldarr['x1']['last'] = $arr['e1']['last'];
        $oldarr['x1']['now'] = $arr['e1']['now'];
        $oldarr['x2']['last'] = $arr['e2']['last'];
        $oldarr['x2']['now'] = $arr['e2']['now'];
        $oldarr['x3']['last'] = $arr['e3']['last'];
        $oldarr['x3']['now'] = $arr['e3']['now'];
        $oldarr['x7']['last'] = $arr['e5']['last'];
        $oldarr['x7']['now'] = $arr['e5']['now'];
        $oldarr['x8']['last'] = $arr['e6']['last'];
        $oldarr['x8']['now'] = $arr['e6']['now'];
        $oldarr['x9']['last'] = $arr['e7']['last'];
        $oldarr['x9']['now'] = $arr['e7']['now'];
        $oldarr['x10']['last'] = $arr['e8']['last'];
        $oldarr['x10']['now'] = $arr['e8']['now'];
        $oldarr['x11']['last'] = $arr['e9']['last'];
        $oldarr['x11']['now'] = $arr['e9']['now'];
        $oldarr['x12']['last'] = $arr['e10']['last'];
        $oldarr['x12']['now'] = $arr['e10']['now'];
        $oldarr['x13']['last'] = $arr['e11']['last'];
        $oldarr['x13']['now'] = $arr['e11']['now'];
        $oldarr['x14']['last'] = $arr['e12']['last'];
        $oldarr['x14']['now'] = $arr['e12']['now'];
        $oldarr['x15']['last'] = $arr['e13']['last'];
        $oldarr['x15']['now'] = $arr['e13']['now'];
        $oldarr['x16']['last'] = $arr['e14']['last'];
        $oldarr['x16']['now'] = $arr['e14']['now'];
        $oldarr['x17']['last'] = $arr['e15']['last'];
        $oldarr['x17']['now'] = $arr['e15']['now'];
        $oldarr['x18']['last'] = $arr['e16']['last'];
        $oldarr['x18']['now'] = $arr['e16']['now'];
        $oldarr['x29']['last'] = $arr['e17']['last'];
        $oldarr['x29']['now'] = $arr['e17']['now'];
        $oldarr['x30']['last'] = $arr['e18']['last'];
        $oldarr['x30']['now'] = $arr['e18']['now'];
        $oldarr['x31']['last'] = $arr['e19']['last'];
        $oldarr['x31']['now'] = $arr['e19']['now'];
        $oldarr['x32']['last'] = $arr['e20']['last'];
        $oldarr['x32']['now'] = $arr['e20']['now'];
        $oldarr['x33']['last'] = $arr['e21']['last'];
        $oldarr['x33']['now'] = $arr['e21']['now'];
        $oldarr['x34']['last'] = $arr['e22']['last'];
        $oldarr['x34']['now'] = $arr['e22']['now'];
        $oldarr['x35']['last'] = $arr['e23']['last'];
        $oldarr['x35']['now'] = $arr['e23']['now'];
        $oldarr['x36']['last'] = $arr['e24']['last'];
        $oldarr['x36']['now'] = $arr['e24']['now'];

        $oldarr['x19']['last'] = $array['f5']['last'];
        $oldarr['x19']['now'] = $array['f5']['now'];
        $oldarr['x20']['last'] = $array['f6']['last'];
        $oldarr['x20']['now'] = $array['f6']['now'];
        $oldarr['x21']['last'] = $array['f7']['last'];
        $oldarr['x21']['now'] = $array['f7']['now'];
        $oldarr['x22']['last'] = $array['f8']['last'];
        $oldarr['x22']['now'] = $array['f8']['now'];
        $oldarr['x23']['last'] = $array['f9']['last'];
        $oldarr['x23']['now'] = $array['f9']['now'];
        $oldarr['x24']['last'] = $array['f10']['last'];
        $oldarr['x24']['now'] = $array['f10']['now'];
        $oldarr['x25']['last'] = $array['f11']['last'];
        $oldarr['x25']['now'] = $array['f11']['now'];
        $oldarr['x26']['last'] = $array['f12']['last'];
        $oldarr['x26']['now'] = $array['f12']['now'];
        $oldarr['x27']['last'] = $array['f13']['last'];
        $oldarr['x27']['now'] = $array['f13']['now'];
        $oldarr['x28']['last'] = $array['f14']['last'];
        $oldarr['x28']['now'] = $array['f14']['now'];
        $tableinfo = json_encode($oldarr,JSON_UNESCAPED_UNICODE);

        $this->assign('tableinfo',$tableinfo);
        $this->assign('title',$data['title']);
        $this->assign('remark',$data['remark']);
        $this->assign('list',$oldarr);
    }
    public function wuliu($tableid,$year,$quarter){
        $obj = D('B2c');
        $data = $obj->table('xf_total_table')->where('id=3')->find();
        $oldarr = json_decode(trim($data['info']),true);
        //港区运输企业
        $con1=array(
            'year'=>$year,
            'quarter'=>$quarter,
            'state'=>3,
            'code'=>'CDTLG409'
        );
        //港区仓储企业
        $con2=array(
            'year'=>$year,
            'quarter'=>$quarter,
            'state'=>3,
            'code'=>'CDTLG410'
        );
        //港区货代企业
        $con3=array(
            'year'=>$year,
            'quarter'=>$quarter,
            'state'=>3,
            'code'=>'CDTLG411'
        );
        $con4=array(
            'year'=>$year,
            'quarter'=>$quarter,
            'state'=>3,
            'code'=>'CDTLG412'
        );
        $res1 = $obj->table('xf_total')->where($con1)->select();
        $res2 = $obj->table('xf_total')->where($con2)->select();
        $res3 = $obj->table('xf_total')->where($con3)->select();
        $res4 = $obj->table('xf_total')->where($con4)->select();
        if (empty($res1) && empty($res2) && empty($res3) && empty($res4)){
            $this->error('该季度暂无报表',0,0);
        }

        //定义三个空数组用于存放各自表的汇总数据
        $arr1 = [];
        $arr2 = [];
        $arr3 = [];
        $arr4 = [];
        foreach ($res1 as $v){
            $arr1[$v['comid']]=json_decode($v['finnal_info'],true);
        }
        foreach ($res2 as $v){
            $arr2[$v['comid']]=json_decode($v['finnal_info'],true);
        }
        foreach ($res3 as $v){
            $arr3[$v['comid']]=json_decode($v['finnal_info'],true);
        }
        foreach ($res4 as $v){
            $arr4[$v['comid']]=json_decode($v['finnal_info'],true);
        }
//汇总物流专业市场的从业人员数 营业收入
        $cnt1 = [];
        $cnt2 = [];
        foreach ($arr4 as $xx=>$ff){
            foreach ($ff as $key=>$value){
                foreach ($value as $k=>$v){
                    if ($k == 'code' && $v == '03' ){
                        $array4['num']['last'] += $value['last']*5;
                        $array4['num']['now'] += $value['now']*5;
                    }
                    if ($k == 'code' && $v == '08'){
                        $array4['money']['last'] += $value['last']*5;
                        $array4['money']['now'] += $value['now']*5;
                    }
                }
            }
        }

        //汇总港区运输企业情况
        foreach ($arr1 as $pp=>$oo){
            foreach ($oo as $key=>$value){
                foreach ($value as $k=>$v){
                    if ($k == 'last' && $v!='--'){
                        $array1[$key][$k] += $v;
                    }
                    if ($k == 'now' && $v!='--'){
                        $array1[$key][$k] += $v;
                    }
                }
            }
        }
        //定义仓储企业中的 填报数
        $count = count($arr2);
        //汇总港区仓储配送企业
        foreach ($arr2 as $hh=>$pp){
            foreach ($pp as $key =>$value){
                foreach ($value as $k=>$v){
                        if($key == 'c21'){
                            $word = strtoupper($v);
                            $word = array_filter(explode('、',$word));

                            switch ($k){
                                case 'last':
                                    $cnt1 = array_merge($cnt1,$word);
                                    break;
                                case 'now':
                                    $cnt2 = array_merge($cnt2,$word);
                                    break;
                                default:
                                    break;
                            }
                        }elseif($k =='last' && $v!='--'){

                            if (is_numeric($v)){
                                $array2[$key][$k] += $v;
                            }else{
                                $aa = explode('、',$v);
                                foreach ($aa as $bb){
                                    if (!in_array($bb,$pre1[$key])){
                                        $pre1[$key][] = $bb;
                                        $array2[$key][$k] .= $bb.'、';
                                    }
                                }
                            }
                        }elseif ($k =='now' && $v!='--'){
                            if (is_numeric($v)){
                                $array2[$key][$k] += $v;
                            }else{
                                $cc = explode('、',$v);
                                foreach ($cc as $dd){
                                    if (!in_array($dd,$pre2[$key])){
                                        $pre2[$key][] = $dd;
                                        $array2[$key][$k] .= $v.'、';
                                    }
                                }
                            }
                        }

                }
            }
        }

        $count1 = array_count_values($cnt1);
        $count2 = array_count_values($cnt2);
        $str1 = '';
        $str2 = '';
        foreach ($count1 as $w=>$e){
            $str1 .= $w.':'.$e.'个　';
        }
        foreach ($count2 as $r=>$t){
            $str2 .= $r.':'.$t.'个　';
        }

        $array2['c6']['last'] =$array2['c6']['last']/$count;
        $array2['c6']['now'] =$array2['c6']['now']/$count;
        $array2['c10']['last'] =$array2['c10']['last']/$count;
        $array2['c10']['now'] =$array2['c10']['now']/$count;
        $array2['c21']['last'] =$str1;
        $array2['c21']['now'] =$str2;
        //汇总货代企业的情况
        foreach ($arr3 as $gg=>$jj){
            foreach ($jj as $key=>$value){
                foreach ($value as $k=>$v){
                    if ($k == 'last' && $v!='--'){
                        $array3[$key][$k] += $v;
                    }
                    if ($k == 'now' && $v!='--'){
                        $array3[$key][$k] += $v;
                    }
                }
            }
        }

        $oldarr['z2']['last'] = $array1['b1']['last'];
        $oldarr['z2']['now'] = $array1['b1']['now'];
        $oldarr['z3']['last'] = $array2['c1']['last'];
        $oldarr['z3']['now'] = $array2['c1']['now'];
        $oldarr['z4']['last'] = $array3['a1']['last'];
        $oldarr['z4']['now'] = $array3['a1']['now'];
        $oldarr['z5']['last'] = $array4['num']['last'];
        $oldarr['z5']['now'] = $array4['num']['now'];
        $oldarr['z1']['last'] = $array1['b1']['last']+$array2['c1']['last']+$array3['a1']['last']+$array4['num']['last'];
        $oldarr['z1']['now'] = $array1['b1']['now']+$array2['c1']['now']+$array3['a1']['now']+$array4['num']['now'];
        $oldarr['z7']['last'] = $array1['b2']['last'];
        $oldarr['z7']['now'] = $array1['b2']['now'];
        $oldarr['z8']['last'] = $array2['c2']['last'];
        $oldarr['z8']['now'] = $array2['c2']['now'];
        $oldarr['z9']['last'] = $array3['a2']['last'];
        $oldarr['z9']['now'] = $array3['a2']['now'];
        $oldarr['z10']['last'] = $array4['money']['last'];
        $oldarr['z10']['now'] = $array4['money']['now'];
        $oldarr['z6']['last'] = $array4['money']['last']+$array1['b2']['last']+$array2['c2']['last']+$array3['a2']['last'];
        $oldarr['z6']['now'] = $array4['money']['now']+$array1['b2']['now']+$array2['c2']['now']+$array3['a2']['now'];
        $oldarr['z12']['last'] = $array1['b7']['last'];
        $oldarr['z12']['now'] = $array1['b7']['now'];
        $oldarr['z13']['last'] = $array1['b8']['last'];
        $oldarr['z13']['now'] = $array1['b8']['now'];
        $oldarr['z14']['last'] = $array1['b9']['last'];
        $oldarr['z14']['now'] = $array1['b9']['now'];
        $oldarr['z15']['last'] = $array1['b10']['last'];
        $oldarr['z15']['now'] = $array1['b10']['now'];
        $oldarr['z11']['last'] = $array1['b7']['last']+$array1['b8']['last']+$array1['b9']['last']+$array1['b10']['last'];
        $oldarr['z11']['now'] = $array1['b7']['now']+$array1['b8']['now']+$array1['b9']['now']+$array1['b10']['now'];
        $oldarr['z17']['last'] = $array1['b12']['last'];
        $oldarr['z17']['now'] = $array1['b12']['now'];
        $oldarr['z18']['last'] = $array1['b13']['last'];
        $oldarr['z18']['now'] = $array1['b13']['now'];
        $oldarr['z19']['last'] = $array1['b14']['last'];
        $oldarr['z19']['now'] = $array1['b14']['now'];
        $oldarr['z20']['last'] = $array1['b15']['last'];
        $oldarr['z20']['now'] = $array1['b15']['now'];
        $oldarr['z16']['last'] = $array1['b12']['last']+$array1['b13']['last']+$array1['b14']['last']+$array1['b15']['last'];
        $oldarr['z16']['now'] = $array1['b12']['now']+$array1['b13']['now']+$array1['b14']['now']+$array1['b15']['now'];;
        $oldarr['z22']['last'] = $array1['b17']['last'];
        $oldarr['z22']['now'] = $array1['b17']['now'];
        $oldarr['z23']['last'] = $array1['b18']['last'];
        $oldarr['z23']['now'] = $array1['b18']['now'];
        $oldarr['z24']['last'] = $array1['b19']['last'];
        $oldarr['z24']['now'] = $array1['b19']['now'];
        $oldarr['z25']['last'] = $array1['b20']['last'];
        $oldarr['z25']['now'] = $array1['b20']['now'];
        $oldarr['z21']['last'] = $array1['b17']['last']+$array1['b18']['last']+$array1['b19']['last']+$array1['b20']['last'];
        $oldarr['z21']['now'] = $array1['b17']['now']+$array1['b18']['now']+$array1['b19']['now']+$array1['b20']['now'];
        $oldarr['z26']['last'] = $array2['c6']['last'];
        $oldarr['z26']['now'] = $array2['c6']['now'];
        $oldarr['z27']['last'] = $array2['c7']['last'];
        $oldarr['z27']['now'] = $array2['c7']['now'];
        $oldarr['z28']['last'] = $array2['c8']['last'];
        $oldarr['z28']['now'] = $array2['c8']['now'];
        $oldarr['z29']['last'] = $array2['c9']['last'];
        $oldarr['z29']['now'] = $array2['c9']['now'];
        $oldarr['z30']['last'] = $array2['c10']['last'];
        $oldarr['z30']['now'] = $array2['c10']['now'];
        $oldarr['z31']['last'] = $array2['c11']['last'];
        $oldarr['z31']['now'] = $array2['c11']['now'];
        $oldarr['z32']['last'] = $array2['c12']['last'];
        $oldarr['z32']['now'] = $array2['c12']['now'];
        $oldarr['z33']['last'] = $array2['c13']['last'];
        $oldarr['z33']['now'] = $array2['c13']['now'];
        $oldarr['z34']['last'] = $array2['c14']['last'];
        $oldarr['z34']['now'] = $array2['c14']['now'];
        $oldarr['z35']['last'] = $array2['c15']['last'];
        $oldarr['z35']['now'] = $array2['c15']['now'];
        $oldarr['z36']['last'] = $array2['c16']['last'];
        $oldarr['z36']['now'] = $array2['c16']['now'];
        $oldarr['z37']['last'] = $array2['c17']['last'];
        $oldarr['z37']['now'] = $array2['c17']['now'];
        $oldarr['z38']['last'] = $array2['c18']['last'];
        $oldarr['z38']['now'] = $array2['c18']['now'];
        $oldarr['z39']['last'] = $array2['c19']['last'];
        $oldarr['z39']['now'] = $array2['c19']['now'];
        $oldarr['z40']['last'] = $array2['c20']['last'];
        $oldarr['z40']['now'] = $array2['c20']['now'];
        $oldarr['z41']['last'] = $array2['c21']['last'];
        $oldarr['z41']['now'] = $array2['c21']['now'];
        $oldarr['z42']['last'] = $array3['a6']['last'];
        $oldarr['z42']['now'] = $array3['a6']['now'];
        $oldarr['z43']['last'] = $array3['a7']['last'];
        $oldarr['z43']['now'] = $array3['a7']['now'];
        $oldarr['z44']['last'] = $array3['a8']['last'];
        $oldarr['z44']['now'] = $array3['a8']['now'];
        $oldarr['z45']['last'] = $array3['a9']['last'];
        $oldarr['z45']['now'] = $array3['a9']['now'];
        $oldarr['z46']['last'] = $array3['a10']['last'];
        $oldarr['z46']['now'] = $array3['a10']['now'];
        $oldarr['z47']['last'] = $array3['a11']['last'];
        $oldarr['z47']['now'] = $array3['a11']['now'];
        $oldarr['z48']['last'] = $array3['a12']['last'];
        $oldarr['z48']['now'] = $array3['a12']['now'];
        $oldarr['z49']['last'] = $array3['a13']['last'];
        $oldarr['z49']['now'] = $array3['a13']['now'];
        $oldarr['z50']['last'] = $array3['a14']['last'];
        $oldarr['z50']['now'] = $array3['a14']['now'];
        $oldarr['z51']['last'] = $array3['a15']['last'];
        $oldarr['z51']['now'] = $array3['a15']['now'];

        $oldarr['z52']['last'] = $array1['b36']['last']+$array2['c22']['last']+$array3['a16']['last'];
        $oldarr['z52']['now'] = $array1['b36']['now']+$array2['c22']['now']+$array3['a16']['now'];
        $oldarr['z53']['last'] = $array1['b37']['last']+$array2['c23']['last']+$array3['a17']['last'];
        $oldarr['z53']['now'] = $array1['b37']['now']+$array2['c23']['now']+$array3['a17']['now'];
        $oldarr['z54']['last'] = $array1['b38']['last']+$array2['c24']['last']+$array3['a18']['last'];
        $oldarr['z54']['now'] = $array1['b38']['now']+$array2['c24']['now']+$array3['a18']['now'];
        $oldarr['z55']['last'] = $array1['b39']['last']+$array2['c25']['last']+$array3['a19']['last'];
        $oldarr['z55']['now'] = $array1['b39']['now']+$array2['c25']['now']+$array3['a19']['now'];
        $oldarr['z56']['last'] = $array1['b40']['last']+$array2['c26']['last']+$array3['a20']['last'];
        $oldarr['z56']['now'] = $array1['b40']['now']+$array2['c26']['now']+$array3['a20']['now'];
        $oldarr['z57']['last'] = $array1['b41']['last']+$array2['c27']['last']+$array3['a21']['last'];
        $oldarr['z57']['now'] = $array1['b41']['now']+$array2['c27']['now']+$array3['a21']['now'];
        $oldarr['z58']['last'] = $array1['b42']['last']+$array2['c28']['last']+$array3['a22']['last'];
        $oldarr['z58']['now'] = $array1['b42']['now']+$array2['c28']['now']+$array3['a22']['now'];
        //1 运输  2 仓储 3 货代 4 物流市场
        $tableinfo = json_encode($oldarr,JSON_UNESCAPED_UNICODE);

        $this->assign('tableinfo',$tableinfo);
        $this->assign('title',$data['title']);
        $this->assign('remark',$data['remark']);
        $this->assign('list',$oldarr);
    }
    public function gongye($tableid,$year,$quarter){
        $obj = D('B2c');
        $data = $obj->table('xf_total_table')->where('id=2')->find();
        $oldarr = json_decode(trim($data['info']),true);
//取得汇总表所需的所有数据
        $res = $obj->table('xf_total')->where($con)->select();
        if (empty($res)){
            $this->error('该季度暂无报表记录',0,0);
        }
        //数组用于存放相应的json数组
        $arr = array();
        foreach ($res as $k=>$v){
            array_push($arr,json_decode($v['finnal_info'],true));
        }
        //定义一个空数组  汇总 汇总表所需的共同数据
        $array = array();
        //定义一个空数组用于存放校测重复
        $prearr = array();
        $prearr1 = array();
        foreach ($arr as $xx=>$ff){
            foreach ($ff as $key=>$value){
                foreach ($value as $k=>$v){
                    if ($k == 'last' && $v!='--'){
                        if (is_numeric($v)){
                            $array[$key][$k] += $v;
                        }else{
                            $tt = explode('、',$v);
                            foreach ($tt as $rr){
                                if (!in_array($rr,$prearr[$key])){
                                    $prearr[$key][] = $rr;
                                    $array[$key][$k] .= $rr.'、';
                                }
                            }
                        }
                    }

                    if ($k == 'now' && $v!='--'){
                        if (is_numeric($v)){
                            $array[$key][$k] += $v;
                        }else{
                            $xx = explode('、',$v);
                            foreach ($xx as $rr){
                                if (!in_array($rr,$prearr1[$key])){
                                    $prearr1[$key][] = $rr;
                                    $array[$key][$k] .= $rr.'、';
                                }
                            }
                        }
                    }
                }
            }
        }

        //定义三个空字符串记录相应企业类别的家输
        $str1 = '';//先进制造业 last
        $str4 = '';//now
        $tol1 = array();
        $str2 = '';//高新技术企业
        $str5 = '';//now
        $tol2 = array();
        $str3 = '';//创新性企业
        $str6 = '';//now
        $tol3 = array();
        /*var_dump($array);die;*/
        //先进制造业单类统计
        foreach ($arr as $xx=>$ff){
            foreach ($ff as $key=>$value){
                foreach ($value as $k=>$v){
                    $v = strtolower($v);
                    if ($key=='d5' && $k=='last' && $v !=''){
                        $str1 +=1;
                        $tol1['d1'][$k] += $ff['d1'][$k];
                        $tol1['d2'][$k] += $ff['d2'][$k];
                        $tol1['d3'][$k] += $ff['d3'][$k];
                        $tol1['d4'][$k] += $ff['d4'][$k];
                    }elseif ($key == 'd6' && $k=='last' && $v !=''){
                        $str2 +=1;
                        $tol2['d1'][$k] += $ff['d1'][$k];
                        $tol2['d2'][$k] += $ff['d2'][$k];
                        $tol2['d3'][$k] += $ff['d3'][$k];
                        $tol2['d4'][$k] += $ff['d4'][$k];
                    }

                    if ($key=='d5' && $k=='now' && $v !=''){
                        $str4 +=1;
                        $tol1['d1'][$k] += $ff['d1'][$k];
                        $tol1['d2'][$k] += $ff['d2'][$k];
                        $tol1['d3'][$k] += $ff['d3'][$k];
                        $tol1['d4'][$k] += $ff['d4'][$k];
                    }elseif ($key == 'd6' && $k=='now' && $v !=''){
                        $str5 +=1;
                        $tol2['d1'][$k] += $ff['d1'][$k];
                        $tol2['d2'][$k] += $ff['d2'][$k];
                        $tol2['d3'][$k] += $ff['d3'][$k];
                        $tol2['d4'][$k] += $ff['d4'][$k];
                    }
                }
            }
        }
        $oldarr['y1']['last'] = $array['d1']['last'];
        $oldarr['y1']['now'] = $array['d1']['now'];
        $oldarr['y2']['last'] = $array['d2']['last'];
        $oldarr['y2']['now'] = $array['d2']['now'];
        $oldarr['y3']['last'] = $array['d3']['last'];
        $oldarr['y3']['now'] = $array['d3']['now'];
        $oldarr['y4']['last'] = $array['d4']['last'];
        $oldarr['y4']['now'] = $array['d4']['now'];
        $oldarr['y10']['last'] = $str1;
        $oldarr['y10']['now'] = $str4;
        $oldarr['y11']['last'] = $tol1['d1']['last'];
        $oldarr['y11']['now'] = $tol1['d1']['now'];
        $oldarr['y12']['last'] = $tol1['d2']['last'];
        $oldarr['y12']['now'] = $tol1['d2']['now'];
        $oldarr['y13']['last'] = $tol1['d3']['last'];
        $oldarr['y13']['now'] = $tol1['d3']['now'];
        $oldarr['y14']['last'] = $tol1['d4']['last'];
        $oldarr['y14']['now'] = $tol1['d4']['now'];

        $oldarr['y15']['last'] = $str2;
        $oldarr['y15']['now'] = $str5;
        $oldarr['y16']['last'] = $tol2['d1']['last'];
        $oldarr['y16']['now'] = $tol2['d1']['now'];
        $oldarr['y17']['last'] = $tol2['d2']['last'];
        $oldarr['y17']['now'] = $tol2['d2']['now'];
        $oldarr['y18']['last'] = $tol2['d3']['last'];
        $oldarr['y18']['now'] = $tol2['d3']['now'];
        $oldarr['y19']['last'] = $tol2['d4']['last'];
        $oldarr['y19']['now'] = $tol2['d4']['now'];


        $oldarr['y25']['last'] = $array['d9']['last'];
        $oldarr['y25']['now'] = $array['d9']['now'];
        $oldarr['y26']['last'] = $array['d10']['last'];
        $oldarr['y26']['now'] = $array['d10']['now'];
        $oldarr['y27']['last'] = $array['d11']['last'];
        $oldarr['y27']['now'] = $array['d11']['now'];
        $oldarr['y28']['last'] = $array['d12']['last'];
        $oldarr['y28']['now'] = $array['d12']['now'];

        $oldarr['y29']['last'] = $array['d13']['last'];
        $oldarr['y29']['now'] = $array['d13']['now'];
        $oldarr['y30']['last'] = $array['d14']['last'];
        $oldarr['y30']['now'] = $array['d14']['now'];
        $oldarr['y31']['last'] = $array['d15']['last'];
        $oldarr['y31']['now'] = $array['d15']['now'];
        $oldarr['y32']['last'] = $array['d16']['last'];
        $oldarr['y32']['now'] = $array['d16']['now'];

        $oldarr['y33']['last'] = $array['d17']['last'];
        $oldarr['y33']['now'] = $array['d17']['now'];
        $oldarr['y34']['last'] = $array['d18']['last'];
        $oldarr['y34']['now'] = $array['d18']['now'];
        $oldarr['y35']['last'] = $array['d19']['last'];
        $oldarr['y35']['now'] = $array['d19']['now'];
        $oldarr['y36']['last'] = $array['d20']['last'];
        $oldarr['y36']['now'] = $array['d20']['now'];
        $oldarr['y37']['last'] = $array['d21']['last'];
        $oldarr['y37']['now'] = $array['d21']['now'];
        $oldarr['y38']['last'] = $array['d22']['last'];
        $oldarr['y38']['now'] = $array['d22']['now'];
        $oldarr['y39']['last'] = $array['d23']['last'];
        $oldarr['y39']['now'] = $array['d23']['now'];
        $oldarr['y40']['last'] = $array['d24']['last'];
        $oldarr['y40']['now'] = $array['d24']['now'];
        $oldarr['y41']['last'] = $array['d25']['last'];
        $oldarr['y41']['now'] = $array['d25']['now'];
        $oldarr['y42']['last'] = $array['d26']['last'];
        $oldarr['y42']['now'] = $array['d26']['now'];
        $oldarr['y43']['last'] = $array['d27']['last'];
        $oldarr['y43']['now'] = $array['d27']['now'];
        $oldarr['y44']['last'] = $array['d28']['last'];
        $oldarr['y44']['now'] = $array['d28']['now'];
        $tableinfo = json_encode($oldarr,JSON_UNESCAPED_UNICODE);

        $this->assign('tableinfo',$tableinfo);
        $this->assign('title',$data['title']);
        $this->assign('remark',$data['remark']);
        $this->assign('list',$oldarr);
    }

    public function down_now(){
        $year = I('year');
        $quarter = I('quarter');
        $data1 = I('tableinfo');
        $tableid = I('tableid');
        if ($tableid==1) {
            $title = "贸易汇总表";
        }elseif ($tableid==2) {
            $title = "工业汇总表";
        }elseif ($tableid == 3) {
            $title = "物流汇总表";
        }
        $data = json_decode(htmlspecialchars_decode($data1),true);

        $filename = $title.$year.'年'.$quarter.'季度';
        //导出数据
        upload_exl($data,$filename,$quarter,$title);
    }
    //生成物流汇总表
    public function total_three($tableid,$year,$quarter){
        $obj = D('B2c');
        //港区运输企业
        $con1=array(
            'year'=>$year,
            'quarter'=>$quarter,
            'state'=>3,
            'code'=>'CDTLG409'
        );
        //港区仓储企业
        $con2=array(
            'year'=>$year,
            'quarter'=>$quarter,
            'state'=>3,
            'code'=>'CDTLG410'
        );
        //港区货代企业
        $con3=array(
            'year'=>$year,
            'quarter'=>$quarter,
            'state'=>3,
            'code'=>'CDTLG411'
        );
        $con4=array(
            'year'=>$year,
            'quarter'=>$quarter,
            'state'=>3,
            'code'=>'CDTLG412'
        );
        //取得模板数据
        $old = $obj->table('xf_total_table')->where(array('id'=>$tableid))->find();
        $oldarr = json_decode(strval(trim($old['info'])),true);
        $res1 = $obj->table('xf_total')->where($con1)->select();
        $res2 = $obj->table('xf_total')->where($con2)->select();
        $res3 = $obj->table('xf_total')->where($con3)->select();
        $res4 = $obj->table('xf_total')->where($con4)->select();
        if (empty($res1) && empty($res2) && empty($res3) && empty($res4)){
            $this->error('该季度暂无报表',0,0);
        }

        //定义三个空数组用于存放各自表的汇总数据
        $arr1 = [];
        $arr2 = [];
        $arr3 = [];
        $arr4 = [];
        foreach ($res1 as $v){
            $arr1[$v['comid']]=json_decode($v['finnal_info'],true);
        }
        foreach ($res2 as $v){
            $arr2[$v['comid']]=json_decode($v['finnal_info'],true);
        }
        foreach ($res3 as $v){
            $arr3[$v['comid']]=json_decode($v['finnal_info'],true);
        }
        foreach ($res4 as $v){
            $arr4[$v['comid']]=json_decode($v['finnal_info'],true);
        }
        //向汇总表加入数据
        $z_arr = [];  //定义汇总表模板每条
        for($i=1;$i<57;$i++){
            $z_arr[$i]='';
        }
        //定义一个该表的总数据空数组
        $zz_arr = [];
        //将409表  arr1  加入到汇总数组 并生成条数
        foreach ($arr1 as $k=>$v) {
            $info = $obj->table('tb_company')->where('ComID='.$k)->field('Name,type')->find();
            $list = $z_arr;
            switch ($info['type']) {
                case 1:
                    $type = '港区规模以上贸易业';
                    break;
                case 2:
                    $type = '港区物流专业市场';
                    break;
                case 3:
                    $type = '临港规模以上工业';
                    break;
                case 4:
                    $type = '港区运输企业';
                    break;
                case 5:
                    $type = '港区仓储配送企业';
                    break;                    
                default:
                    $type = '其他';
                    break;
            }
            $list['1'] = $info['name'];
            $list['2'] = $type;
            $list['3'] = $v['b1']['now'];
            $list['7'] = $v['b2']['now'];

            //合计
            $list['11'] = $v['b7']['now']+$v['b8']['now']+$v['b9']['now']+$v['b10']['now'];
            $list['12'] = $v['b7']['now'];
            $list['13'] = $v['b8']['now'];
            $list['14'] = $v['b9']['now'];
            $list['15'] = $v['b10']['now'];

            //合计
            $list['16'] = $v['b12']['now']+$v['b13']['now']+$v['b14']['now']+$v['b15']['now'];
            $list['17'] = $v['b12']['now'];
            $list['18'] = $v['b13']['now'];
            $list['19'] = $v['b14']['now'];
            $list['20'] = $v['b15']['now'];

            $list['21'] = $v['b17']['now']+$v['b18']['now']+$v['b19']['now']+$v['b20']['now'];
            $list['22'] = $v['b17']['now'];
            $list['23'] = $v['b18']['now'];
            $list['24'] = $v['b19']['now'];
            $list['25'] = $v['b20']['now'];

            $list['50'] = $v['b37']['now']+$v['b38']['now'];
            $list['51'] = $v['b37']['now'];
            $list['52'] = $v['b38']['now'];
            $list['53'] = $v['b39']['now'];
            $list['54'] = $v['b40']['now'];
            $list['55'] = $v['b41']['now'];
            $list['56'] = $v['b42']['now'];

            //向汇总加入
            $zz_arr[] = $list;
            $data1[] = $list;
        }
        //409表数据求平均
        foreach ($data1 as $value) {
            foreach (array_filter($value) as $k=>$v) {
                $mean1[$k] +=$v;
            }
        }
        $count1 = count($data1);
        foreach ($mean1 as $k=>$v) {
            $n_mean1[$k] =$v/$count1;
        }


        //将410表数据加入
            foreach ($arr2 as $k=>$v) {
            $info = $obj->table('tb_company')->where('ComID='.$k)->field('Name,type')->find();
            $list = $z_arr;
            switch ($info['type']) {
                case 1:
                    $type = '港区规模以上贸易业';
                    break;
                case 2:
                    $type = '港区物流专业市场';
                    break;
                case 3:
                    $type = '临港规模以上工业';
                    break;
                case 4:
                    $type = '港区运输企业';
                    break;
                case 5:
                    $type = '港区仓储配送企业';
                    break;                    
                default:
                    $type = '其他';
                    break;
            }
            $list['1'] = $info['name'];
            $list['2'] = $type;
            $list['4'] = $v['c1']['now'];
            $list['8'] = $v['c2']['now'];
            $list['26'] = $v['c6']['now'];
            $list['27'] = $v['c7']['now'];
            $list['28'] = $v['c8']['now'];
            $list['29'] = $v['c9']['now'];
            $list['30'] = $v['c10']['now'];

            $list['31'] = $v['c12']['now']+$v['c13']['now'];
            $list['32'] = $v['c12']['now'];
            $list['33'] = $v['c13']['now'];

            $list['34'] = $v['c14']['now']+$v['c15']['now'];
            $list['35'] = $v['c14']['now'];
            $list['36'] = $v['c15']['now'];
            
            $list['37'] = $v['c17']['now'];
            $list['38'] = $v['c18']['now'];
            $list['39'] = $v['c19']['now'];
            $list['40'] = $v['c20']['now'];
            $list['41'] = $v['c21']['now'];

            $list['50'] = $v['c23']['now']+$v['c24']['now'];
            $list['51'] = $v['c23']['now'];
            $list['52'] = $v['c24']['now'];
            $list['53'] = $v['c25']['now'];
            $list['54'] = $v['c26']['now'];
            $list['55'] = $v['c27']['now'];
            $list['56'] = $v['c28']['now'];

            //向汇总加入
            $zz_arr[] = $list;
            $data2[] = $list;
        }

        foreach ($data2 as $value) {
            foreach (array_filter($value) as $k=>$v) {
                $mean2[$k] +=$v;
            }
        }
        $count2 = count($data2);
        foreach ($mean2 as $k=>$v) {
            $n_mean2[$k] =$v/$count2;
        }

        //将411表加入汇总表
        foreach ($arr3 as $k=>$v) {
            $info = $obj->table('tb_company')->where('ComID='.$k)->field('Name,type')->find();
            $list = $z_arr;
            switch ($info['type']) {
                case 1:
                    $type = '港区规模以上贸易业';
                    break;
                case 2:
                    $type = '港区物流专业市场';
                    break;
                case 3:
                    $type = '临港规模以上工业';
                    break;
                case 4:
                    $type = '港区运输企业';
                    break;
                case 5:
                    $type = '港区仓储配送企业';
                    break;                    
                default:
                    $type = '其他';
                    break;
            }
            $list['1'] = $info['name'];
            $list['2'] = $type;
            $list['5'] = $v['a1']['now'];
            $list['9'] = $v['a2']['now'];
            $list['42'] = $v['a7']['now'];
            $list['43'] = $v['a8']['now'];
            $list['44'] = $v['a9']['now'];
            $list['45'] = $v['a10']['now'];
            $list['46'] = $v['a12']['now'];
            $list['47'] = $v['a13']['now'];
            $list['48'] = $v['a14']['now'];
            $list['49'] = $v['a15']['now'];

            $list['50'] = $v['a17']['now']+$v['a18']['now'];
            $list['51'] = $v['a17']['now'];
            $list['52'] = $v['a18']['now'];
            $list['53'] = $v['a19']['now'];
            $list['54'] = $v['a20']['now'];
            $list['55'] = $v['a21']['now'];
            $list['56'] = $v['a22']['now'];

            //向汇总加入
            $zz_arr[] = $list;
            $data3[] = $list; 
        }

        foreach ($data3 as $value) {
            foreach (array_filter($value) as $k=>$v) {
                $mean3[$k] +=$v;
            }
        }
        $count3 = count($data3);
        foreach ($mean3 as $k=>$v) {
            $n_mean3[$k] =$v/$count3;
        }


        foreach ($arr4 as $k=>$v) {
            $info = $obj->table('tb_company')->where('ComID='.$k)->field('Name,type')->find();
            $list = $z_arr;
            switch ($info['type']) {
                case 1:
                    $type = '港区规模以上贸易业';
                    break;
                case 2:
                    $type = '港区物流专业市场';
                    break;
                case 3:
                    $type = '临港规模以上工业';
                    break;
                case 4:
                    $type = '港区运输企业';
                    break;
                case 5:
                    $type = '港区仓储配送企业';
                    break;                    
                default:
                    $type = '其他';
                    break;
            }
            $list['1'] = $info['name'];
            $list['2'] = $type;
            $list['6'] = $v['shop_num']['now']*5;
            $list['10'] = $v['market_income']['now']*5;
            //向汇总加入
            $zz_arr[] = $list;
            $data4[] = $list;
        }

        foreach ($data4 as $value) {
            foreach (array_filter($value) as $k=>$v) {
                $mean4[$k] +=$v;
            }
        }
        $count4 = count($data4);
        foreach ($mean4 as $k=>$v) {
            $n_mean4[$k] =$v/$count4;
        }
        //得到平均数据列
        $mean = $n_mean1+$n_mean2+$n_mean3+$n_mean4;
        ksort($mean);
        //汇总数据列
        $total = [];
        $a = 0;
        $b = 0;
        $c = 0;
        foreach ($zz_arr as $value) {
            foreach ($value as $k=>$v) {
                if ($k==41) {
                    if (strtoupper($v) =='A') {
                        $a +=1;                  
                    }elseif (strtoupper($v) =='B') {
                        $b +=1;
                    }elseif (strtoupper($v) =='C'){
                        $c +=1;
                    }               
                }elseif ($k==37) {
                    $total[$k] .= $v.'、';
                }else{
                    $total[$k] += $v;
                }
            }
        }
        $p_count = count($arr1)+count($arr3)+count($arr2);
        //将汇总列加入汇总数据中
        $total['1'] = '汇总';
        $total['2'] = '';
        $total['41'] = 'A:'.$a.'个 B:'.$b.'个 C:'.$c.'个';
        $savearr = explode('、',$total['37']);
        $total['37'] = implode('、',array_unique(array_filter($savearr)));
        ksort($total);
        $mean['1'] = '平均';
        $mean['2'] = '';
        $mean['50'] = $total['50']/$p_count;
        $mean['51'] = $total['51']/$p_count;
        $mean['52'] = $total['52']/$p_count;
        $mean['53'] = $total['53']/$p_count;
        $mean['54'] = $total['54']/$p_count;
        $mean['55'] = $total['55']/$p_count;
        $mean['56'] = $total['56']/$p_count;
        $cellarr = [3,4,5,6,50,51,52,53,54,55,56];
        foreach ($cellarr as $value) {
            $mean[$value] = ceil($mean[$value]);
        }        
        $zz_arr[] = $mean;
        $zz_arr[] = $total;
        
        wuliu_upload($zz_arr,$year.'年'.$quarter.'季度详细物流汇总表',2,'物流汇总表');
        /*var_dump($zz_arr);*/
    }

    //生成工业汇总表
    public function total_two($tableid,$year,$quarter){

        $obj = D('B2c');
        $tableid = $tableid;
        $con['year'] = $year;
        $con['quarter'] = $quarter;
        $con['state'] = 3;
        $con['code'] ='CDTLG414';
        //取得模板数据
        $old = $obj->table('xf_total_table')->where(array('id'=>$tableid))->find();
        $oldarr = json_decode(strval(trim($old['info'])),true);
        //取得汇总表所需的所有数据
        $res = $obj->table('xf_total')->where($con)->select();
        if (empty($res)){
            $this->error('该季度暂无报表记录',0,0);
        }
        //数组用于存放相应的json数组
        $arr = array();
        foreach ($res as $k=>$v){
            $arr[$v['comid']]=json_decode($v['finnal_info'],true);
        }
        $g_arr = [];  //定义汇总表模板每条
        for($i=1;$i<36;$i++){
            $g_arr[$i]='';
        }        
        //定义一个空数组  汇总 汇总表所需的共同数据
        $array = array();
        //定义一个空数组用于存放校测重复
        $prearr = array();
        $prearr1 = array();
        foreach ($arr as $key=>$value) {      
            $list = $g_arr;
            $info = $obj->table('tb_company')->where("ComID=".$key)->field('Name,type')->find();
            $list['1'] = $info['name'];
            $list['2'] = $value['d1']['now'];              
            $list['3'] = $value['d2']['now'];
            $list['4'] = $value['d3']['now'];
            $list['5'] = $value['d4']['now'];
            $list['6'] = $value['d5']['now'];
            $list['11'] = $value['d6']['now'];
            $list['16'] = $value['d9']['now'];
            $list['17'] = $value['d10']['now'];
            $list['18'] = $value['d11']['now'];
            $list['19'] = $value['d12']['now'];
            $list['20'] = $value['d13']['now'];
            $list['21'] = $value['d14']['now'];
            $list['22'] = $value['d15']['now'];
            $list['23'] = $value['d16']['now'];
            $list['24'] = $value['d18']['now']+$value['d19']['now']+$value['d20']['now'];
            $list['25'] = $value['d18']['now'];
            $list['26'] = $value['d19']['now'];
            $list['27'] = $value['d20']['now'];

            $list['28'] = $value['d22']['now']+$value['d23']['now']+$value['d24']['now'];
            $list['29'] = $value['d22']['now'];
            $list['30'] = $value['d23']['now'];
            $list['31'] = $value['d24']['now'];

            $list['32'] = $value['d26']['now']+$value['d27']['now']+$value['d28']['now'];
            $list['33'] = $value['d26']['now'];
            $list['34'] = $value['d27']['now'];
            $list['35'] = $value['d28']['now'];
            $array[] = $list;                
        }
        //两个类别小项通缉
        $xj = [];
        $zn = [];
        //生成汇总数据和平均数据
        //汇总
        $total = $g_arr;
        foreach ($array as $value) {
            foreach ($value as $k=>$v) {
                if ($k == 6 && $v !='') {
                    $prearr[] = $v;
                    $xj[] = [$value[2],$value[3],$value[4],$value[5]];
                }elseif ($k == 11 && $v !='') {
                    $prearr1[] = $v;
                    $zn[] = [$value[2],$value[3],$value[4],$value[5]];
                }elseif ($k == 16 && $v !='') {
                    $total[$k] .= $v.'、'; 
                }elseif ($k == 18 && $v !='') {
                    $total[$k] .= $v.'、'; 
                }elseif ($k == 19 && $v !='') {
                    $total[$k] .= $v.'、';  
                }elseif ($k == 20 && $v !='') {
                    $total[$k] .= $v.'、'; 
                }elseif ($k == 22 && $v !='') {
                    $total[$k] .= $v.'、'; 
                }elseif ($k == 23 && $v !='') {
                    $total[$k] .= $v.'、'; 
                }elseif ($v!='' && is_numeric($v)) {
                    $total[$k] += $v;
                }      
            }
        }
        foreach (array_count_values($prearr) as $k=>$v) {
            $total['6'] .= $k.':'.$v.'个 ';
        }
        foreach (array_count_values($prearr1) as $k=>$v) {
            $total['11'] .= $k.':'.$v.'个 ';
        }
        $numarr = [16,18,19,20,22,23];
        foreach ($numarr as $value) {
            $savearr = explode('、',$total[$value]);
            $total[$value] = implode('、',array_unique(array_filter($savearr)));
        }
        foreach ($xj as $value) {
            foreach ($value as $k=>$v) {
                $total[7+$k] += $v;
            }
        }
        foreach ($zn as $value) {
            foreach ($value as $k=>$v) {
                $total[12+$k] += $v;
            }
        }
        //平均数据
        $mean = $g_arr;
        $u_count = count($res);
        foreach ($total as $k=>$v) {
            $mean[$k] = $v/$u_count;
        }
        $mean['1'] = '平均';
        $total['1'] = '汇总';
        $cellarr = [2,7,12];
        foreach ($cellarr as $value) {
            $mean[$value] = ceil($mean[$value]);
        }        
        $array[] = $mean;
        $array[] = $total;
        gongye_upload($array,$year.'年'.$quarter.'季度详细工业汇总表',2,'工业汇总表');     

    }

    //生成贸易汇总表
    public function total_one($tableid,$year,$quarter){
        $obj = D('B2c');
        $tableid = $tableid;
        $con2['year'] = $year;
        $con2['quarter'] = $quarter;
        $con2['code'] ='CDTLG417';
        $con2['state'] = 3;
        $con1['year'] = $year;
        $con1['quarter'] = $quarter;
        $con1['code'] ='CDTLG416';
        $con1['state'] = 3;
        //取出模板数据
        $old = $obj->table('xf_total_table')->where(array('id'=>$tableid))->find();
        $oldarr = json_decode(strval(trim($old['info'])),true);
        //查询出两张表所需的记录
        $res1 = $obj->table('xf_total')->where($con1)->order('code asc')->select();
        $res2 = $obj->table('xf_total')->where($con2)->order('code asc')->select();
        if (empty($res1) && empty($res2)){
            $this->error('该季度暂无汇总数据',0,0);
        }
        $arr1 = array();
        $arr2 = array();
        //将第一种表数据加入一种数组中
        foreach ($res1 as $k=>$v){
            $arr1[$v['comid']] = json_decode($v['finnal_info'],true);
        }
        foreach ($res2 as $k=>$v){
            $arr2[$v['comid']] = json_decode($v['finnal_info'],true);
        }
        //16表集合结果
        $arr = array();
        $mean1 = array();
        $mean2 = array();
        //定义汇总数据列
        $m_arr = [];
        for($i=1;$i<36;$i++){
            $m_arr[$i]='';
        }
        //汇总数组
        $array = [];
        //将416表的数据列加入
        foreach ($arr1 as $key=>$value) {
            $list = $m_arr;
            $info = $obj->table('tb_company')->where('ComID='.$key)->field('Name,type')->find();
            $list['1'] = $info['name'];
            switch ($info['type']) {
                case 1:
                    $type = '港区规模以上贸易业';
                    break;
                case 2:
                    $type = '港区物流专业市场';
                    break;
                case 3:
                    $type = '临港规模以上工业';
                    break;
                case 4:
                    $type = '港区运输企业';
                    break;
                case 5:
                    $type = '港区仓储配送企业';
                    break;                    
                default:
                    $type = '其他';
                    break;
            }
            $list['2'] = $type;
            $list['3'] = $value['e1']['now'];
            $list['4'] = $value['e2']['now'];
            $list['5'] = $value['e3']['now'];
            for($i=6;$i<26;$i++){
                $list[$i] = $value['e'.($i-1)]['now'];
            }
            $array[] = $list;
            $data1[] =$list;
        } 

        foreach ($arr2 as $k=>$v) {
             $list = $m_arr;
             $info = $obj->table('tb_company')->where('ComID='.$k)->field('Name,type')->find();
             $list['1'] = $info['name'];
                         switch ($info['type']) {
                case 1:
                    $type = '港区规模以上贸易业';
                    break;
                case 2:
                    $type = '港区物流专业市场';
                    break;
                case 3:
                    $type = '临港规模以上工业';
                    break;
                case 4:
                    $type = '港区运输企业';
                    break;
                case 5:
                    $type = '港区仓储配送企业';
                    break;                    
                default:
                    $type = '其他';
                    break;
            }
            $list['2'] = $type;
            $list['3'] = $v['f1']['now'];
            $list['4'] = $v['f2']['now'];
            $list['5'] = $v['f3']['now'];
            for($i=26;$i<36;$i++){
                $list[$i] = $v['f'.($i-21)]['now'];
            }
            $array[] = $list;
            $data2[] = $list;
         } 
         $total = $m_arr;
         foreach ($array as $value) {
             foreach ($value as $k=>$v) {
                 if ($k == 8 && $v!='') {
                     $total[$k] .= $v.'、';
                 }elseif ($k == 11 && $v!='') {
                     $total[$k] .= $v.'、';
                 }elseif ($k == 14 && $v!='') {
                     $total[$k] .= $v.'、';
                 }elseif ($k == 17 && $v!='') {
                     $total[$k] .= $v.'、';
                 }elseif ($k == 20 && $v!='') {
                     $total[$k] .= $v.'、';
                 }elseif ($k == 23 && $v!='') {
                     $total[$k] .= $v.'、';
                 }elseif ($k == 31 && $v!='') {
                     $total[$k] .= $v.'、';
                 }elseif ($v!='' && is_numeric($v)) {
                     $total[$k] += $v;
                 }
             }
         }
        $numarr = [8,11,14,17,20,23,31];
        foreach ($numarr as $value) {
            $savearr = explode('、',$total[$value]);
            $total[$value] = implode('、',array_unique(array_filter($savearr)));
        }
        
        $count1 = count($arr1);
        $count2 = count($arr2);
        //平均数据列
        $mean = $m_arr;
        foreach ($data1 as $value) {
            foreach ($value as $k=>$v) {
                $mean1[$k] +=$v;
            }
        }
        for($i=6;$i<26;$i++){
            $mean[$i] = $mean1[$i]/$count1;
        }
        foreach ($data2 as $value) {
            foreach ($value as $k=>$v) {
                $mean2[$k] +=$v;
            }
        }
        for($i=26;$i<36;$i++){
            $mean[$i] = $mean2[$i]/$count1;
        }
        $mean['3'] = $total['3']/($count1+$count2);
        $mean['4'] = $total['5']/($count1+$count2);
        $mean['5'] = $total['5']/($count1+$count2);
        $mean['1'] = '平均';
        $total['1'] = '汇总';
        $cellarr = [3,27];
        foreach ($cellarr as $value) {
            $mean[$value] = ceil($mean[$value]);
        }
        $array[] = $mean;
        $array[] = $total;

        maoyi_upload($array,$year.'年'.$quarter.'季度详细贸易汇总表',2,'贸易汇总表');     
    }


}